<?php
/**
 * Users controller class
 */
class Page_AdminController extends Zend_Controller_Action
{
	public function init()
	{
		$user = new Member_Service_User();

		if(!$user->getIdentity()) {
			if($this->getRequest()->getActionName()!='access-denied')
			$this->_helper->redirector('access-denied');
		}
	}

	public function accessDeniedAction()
	{

	}

	/**
	 * Ajoute un matériel
	 * @return unknown_type
	 */
	public function addPageAction()
	{
		$form = new Page_Form_Page();
		if($this->getRequest()->isPost()) {
			$postedData = $this->getRequest()->getPost();
			if($form->isValid($postedData)){
				$page = new Page_Model_Page();
				$page->insert($form->getTablePreparedData($postedData));
				//$this->_helper->redirector('access-denied');
			}
		}

		$this->view->form = $form;
		$this->view->layout()->title = "Ajout d'une page";
	}

	/**
	 * Modifie une page
	 * @return unknown_type
	 */
	public function editPageAction()
	{
		$form = new Page_Form_Page();
		$pageId = $this->_getParam('pid',null);
		if($this->getRequest()->isPost()) {
			$postedData = $this->getRequest()->getPost();
			if($form->isValid($postedData)){
				$page = new Page_Model_Page();
				$page->update($form->getTablePreparedData($postedData),'id = ' . (int) $pageId);
				//$this->_helper->redirector('access-denied');
			}
		}
		$form->populateByPageId($pageId);
		$this->view->form = $form;
		$this->view->layout()->title = "Edit page";
	}

	public function deletePageAction()
	{
		$pageId = $this->_getParam('pid',null);
		$page = new Page_Model_Page();
		$page->deleteById($pageId);
	}

	/**
	 * Ajoute un matériel
	 * @return unknown_type
	 */
	public function addCategoryAction()
	{
		$form = new Page_Form_Category();
		if($this->getRequest()->isPost()) {
			$postedData = $this->getRequest()->getPost();
			if($form->isValid($postedData)){
				$category = new Page_Model_Category();
				$category->insert($form->getTablePreparedData($postedData));
				//$this->_helper->redirector('access-denied');
			}
		}

		$this->view->form = $form;
		$this->view->layout()->title = "Ajout d'une catégorie";
	}

	/**
	 * Modifie une catégorie
	 * @return unknown_type
	 */
	public function editCategoryAction()
	{
		$form = new Page_Form_Category();
		$categoryId = $this->_getParam('id',null);
		//echo $categoryId; exit;
		if($categoryId && $this->getRequest()->isPost()) {
			$postedData = $this->getRequest()->getPost();
			if($form->isValid($postedData)){
				$category = new Page_Model_Category();
				$category->update($form->getTablePreparedData($postedData), 'id = ' . (int)$categoryId);
				//$this->_helper->redirector('access-denied');
			}
		}
		$data = $form->populateByCategoryId($categoryId);print_r($data);
		$this->view->form = $form;
		$this->view->layout()->title = "Edit Category";
	}

	public function deleteCategoryAction()
	{
		$categoryId = $this->_getParam('catid',null);
		$category = new Page_Model_Category();
		$category->deleteById($categoryId);
	}

	public function editMenu()
	{

	}

	public function deleteComment()
	{

	}

	public function editComment()
	{

	}

	public function indexAction()
	{
		//$liens
	}

}
